﻿using System;
using System.Threading.Tasks;
using Dpx.Views;
using Xamarin.Forms;

namespace Dpx.Services {
    /// <summary>
    /// 内容页导航服务。
    /// </summary>
    public class ContentNavigationService : IContentNavigationService {
        // ******** 构造函数

        /// <summary>
        /// 内容页面激活服务。
        /// </summary>
        private readonly IContentPageActivationService _contentPageActivationService;

        /// <summary>
        /// 内容页导航服务。
        /// </summary>
        /// <param name="contentPageActivationService">内容页导航服务。</param>
        public ContentNavigationService(
            IContentPageActivationService contentPageActivationService) {
            _contentPageActivationService = contentPageActivationService;
        }

        // ******** 继承方法

        /// <summary>
        /// 导航。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        public async Task NavigateToAsync(string pageKey) {
            await MainPage.Detail.Navigation.PushAsync(
                _contentPageActivationService.Activate(pageKey));
        }

        /// <summary>
        /// 导航。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        /// <param name="parameter">参数。</param>
        public async Task NavigateToAsync(string pageKey, object parameter) {
            // 取出目标页
            var page = _contentPageActivationService.Activate(pageKey);
            // 给目标页设置属性
            NavigationContext.SetParameter(page, parameter);
            // 跳转
            await MainPage.Detail.Navigation.PushAsync(page);
        }

        // ******** 私有变量

        /// <summary>
        /// MainPage。
        /// </summary>
        private static MainPage MainPage => (MainPage) Application.Current.MainPage;
    }

    /// <summary>
    /// 内容导航常量。
    /// </summary>
    public static class ContentNavigationConstant {
        /// <summary>
        /// 推荐详情页键。
        /// </summary>
        public const string TodayDetailPage = nameof(Views.TodayDetailPage);

        /// <summary>
        /// 诗词详情页
        /// </summary>
        public const string DetailPage = nameof(Views.DetailPage);

        /// <summary>
        /// 搜索结果页
        /// </summary>
        public const string ResultPage = nameof(Views.ResultPage);

    }
}